import requests

from urllib.parse import quote
# 价格动态
from bs4 import BeautifulSoup

from database.RedisHelper import RedisHelper
from entity.Keys import Keys
from entity.ListPage import ListPage
from libs.SeleniumHelper import SeleniumHelper

redis_helper=RedisHelper()
cr = SeleniumHelper()
# keys =['客厅灯']
ks = Keys()
keys = ks.getKeys()
for item in keys:

    key= quote(item)
    url='https://search.suning.com/'+key+'/'
    lp = ListPage()
    text =lp.getPage(url)

    lis = lp.getList(text)
    for item in lis:

        try:
            price = cr.findPrice('https:'+item)
            pre_price=redis_helper.get('cc:' + item)
            # print(type(pre_price))
            if pre_price != None:
                if price<pre_price*0.2:
                    print('价格异常')
                    print(item)
                if price ==  pre_price:
                    print('same price')
            redis_helper.set('cc:'+item,price)
        except Exception as e:
            print(e)
        # break